# -*- coding: utf-8 -*-
import json
import requests

# 无法通过接口获取的测试环境的域名
xxcc = ['hrcp.xxx.com', 'hrtp.xxx.com', '361.xxx.com']
# 接口获取域名信息
url = "http://longtan.xxx.cn/cmdb/project/getAllNginxIpAndDomainAndInstanceIp.json"
payload = 'env=online'
headers = {
    'X-Requested-With': 'XMLHttpRequest',
    'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.request("POST", url, headers=headers, data=payload)
data = response.text

# 获取域名写入文件中
data_dict = json.loads(data)
domain_list = xxcc
for k, v in data_dict['data'].items():
    for k1, v1 in v.items():
        for i in v1:
            if i not in domain_list:
                # 域名即将下线，暂不扫描
                if i == 'campus.xxx.com':
                    continue
                else:
                    domain_list.append(i)

with open("awvs.txt", "w") as f:
    for i in domain_list:
        if domain_list.index(i) == len(domain_list)-1:
            f.writelines(i)
        else:
            f.writelines(i + '\n')

# src相关域名获取
new_domain_list = []
for i in domain_list:
    url_list = i.split('.')
    if url_list[-1] == 'com':
        if url_list[-2] == 'xxx' or url_list[-2] == 'xxx':
            new_domain_list.append(i)
with open("src_domain_realtime.txt", "w") as f:
    for i in new_domain_list:
        if new_domain_list.index(i) == len(new_domain_list)-1:
            f.writelines(i)
        else:
            f.writelines(i + '\n')